<?php

namespace App\Listeners;

use App\Events\Application;
use App\Models\Application as ModelsApplication;
use App\Models\ApplicationProcesses;
use App\Models\ApplicationRecord;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class SendApplicationNotification
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  \App\Events\Application  $event
     * @return void
     */
    public function handle(Application $event)
    {

        $Application = ModelsApplication::where('status','rejected')->where('source_id',$event->source_id)->where('model',$event->model)->where('type',$event->type)->where('user_id',$event->user_id)->where('project_id',$event->project_id)->orderBy('id','desc')->first();
        if(empty($Application)){
            $Application = new ModelsApplication();
            $Application->type = $event->type;
            $Application->source = $event->source;
            $Application->model = $event->model;
            $Application->user_id = $event->user_id;
            $Application->source_id = $event->source_id;
            $Application->project_id = $event->project_id;
            $Application->reason = $event->reason ?? '/';
            $Application->payment_type = $event->payment_type ?? '/';
            $Application->pay_info = $event->pay_info ?? '/';
            $Application->price = $event->price ?? 0;
        }else{
            $Application->status = 'pending';
            ApplicationRecord::where('application_id',$Application->id)->update(['is_process' => 0]);
        }

        if($Application->type == 'apply'){
            $Processes = ApplicationProcesses::where('project_id',$event->project_id)->where('level','<',6)->orderBy('level','asc')->get();
        }else{
            $Processes = ApplicationProcesses::where('project_id',$event->project_id)->orderBy('level','asc')->get();
        }

        // $Processes = ApplicationProcesses::where('project_id',$event->project_id)->orderBy('level','asc')->get();
        $record = [];
        $reviewer_id = 0;
        foreach ($Processes as $k => $v) {
            $arr = [];
            // $arr['application_id'] = $Application->id;
            $arr['reviewer_id'] = $v->user_id;
            $arr['level'] = $v->level;
            $arr['role_id'] = $v->role_id;
            $arr['comment'] = '/';
            $arr['is_process'] = 1;
            $arr['created_at'] = Date("Y-m-d H:i:s",time());
            $record[] = $arr;
            if($k == 0){
                $reviewer_id = $v->user_id;
            }
        }
        $Application->reviewer_id = $reviewer_id;
        DB::transaction(function () use ($Application,$record) {
            $Application->save();
            foreach ($record as $k => $v) {
                $record[$k]['application_id'] = $Application->id;
            }
            ApplicationRecord::insert($record);
        });
    }
}
